﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace Maintain_supplier_information
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        SupplierBizlogic sbz; 

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId != 5)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                sbz = new SupplierBizlogic();

                if (!IsPostBack)
                {
                    //DataSet1TableAdapters.s1TableAdapter ta = new DataSet1TableAdapters.s1TableAdapter();
                    //DataSet1.s1DataTable dt = new DataSet1.s1DataTable();
                    //ta.Fill(dt);
                    //GridView1.DataSource = dt;
                    //GridView1.DataBind();
                    //if (ddl_Category.SelectedItem.Text == "All")
                    //{

                    //    GridView1.DataSource = sbz.ret_sta_items(ddl_Category.SelectedValue);
                    //    GridView1.DataBind();
                    //}

                    if (!IsPostBack)
                    {
                        PopulateCategory();
                        GenerateGridView();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }    

        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                GenerateGridView();
            }
            catch (Exception ex)
            {
            }
        }                   

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GridView1.EditIndex = -1;
                GenerateGridView();
            }
            catch (Exception ex)
            {
            }
        }

        //protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    int te = e.RowIndex;
        //    string ItemNumber = ((Label)GridView1.Rows[te].FindControl("Label1")).Text;
        //    StationeryInventoryEntities1 st = new StationeryInventoryEntities1();
        //    Stationery m = st.Stationeries.FirstOrDefault(x => x.ItemCode == ItemNumber); 
        //    SupplierDetail s = st.SupplierDetails.FirstOrDefault(y => y.ItemCode == ItemNumber); 
        //    st.SupplierDetails.Remove(s);    
        //    st.Stationeries.Remove(m);
        //    st.SaveChanges();
        //    GenerateGridView();
        //    DataBind();             
        //}

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int te = e.RowIndex;
                string ItemNumber = ((Label)GridView1.Rows[te].FindControl("Label1")).Text;
                TextBox reorder_level = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
                TextBox reorder_qty = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
                int reorderLvl = Convert.ToInt32(reorder_level.Text);
                int reorderQty = Convert.ToInt32(reorder_qty.Text);
                sbz.UpdateStationeryReorderLvlAndQty(ItemNumber, reorderLvl, reorderQty);
                GenerateGridView();
                GridView1.EditIndex = -1;
                DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GridView1.EditIndex = e.NewEditIndex;
                GenerateGridView();
            }
            catch (Exception ex)
            {
            }
        }        

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Select")
                {
                    int te = Convert.ToInt32(e.CommandArgument);
                    List<object> list = new List<object>();
                    string itemnumber = ((Label)GridView1.Rows[te].FindControl("Label1")).Text;
                    //GridView1.Rows[te].Cells[0].Text;
                    list = sbz.getSelectedSupplierDetail(itemnumber);
                    gdview_details.DataSource = list;
                    gdview_details.DataBind();
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void Btn_AddNewItem_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("~/AddItem.aspx");
            }
            catch (Exception ex)
            {
            }
        }

        private void PopulateCategory()
        {
            ddl_Category.DataSource = sbz.DisplayAllCategory();
            ddl_Category.DataTextField = "CategoryName";
            ddl_Category.DataValueField = "CategoryID";
            ddl_Category.DataBind();
        }

        private void GenerateGridView()
        {
            List<object> list = new List<object>();
            int category;
            category = Convert.ToInt32(ddl_Category.SelectedValue);

            if (ddl_Category.SelectedItem.Text == "All")
            {
                list = sbz.ret_sta_items_all();
            }
            else
            {
                list = sbz.ret_sta_items(category);
            }

            GridView1.DataSource = list;
            GridView1.DataBind();
            gdview_details.DataSource = null;
            gdview_details.DataBind();
        } 

    }
}